﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace Thesis_v01
{
   public partial class GraphResponses : Form
   {
      public GraphResponses()
      {
         InitializeComponent();
      }

      public void CreateGraph(QuestionDetail question, ArrayList responses)
      {
         GraphPane myPane = zgc.GraphPane;
         myPane.CurveList.Clear();
         myPane.GraphObjList.Clear();
         BarSettings bs = myPane.BarSettings;

         // Set the title and axis labels
         myPane.Title.Text = "Responses for Question " + question.Number + "(Right-click on graph for menu)";
         myPane.XAxis.Title.Text = "Totals";
         myPane.YAxis.Title.Text = "Answers";

         if (question.Type == QuestionType.MCQ)
         {
            String[] labels = ParseStringArray(question.PossibleAnswers);
            myPane.YAxis.Scale.TextLabels = labels;
            myPane.YAxis.Type = AxisType.Text;

            AnalyseResponses analysis = new AnalyseResponses();
            ArrayList tallies = analysis.TallyResponses(question.PossibleAnswers, responses);

            //int y = 0;
            //foreach (TallyDetail tally in tallies)
            //{
            //   Console.WriteLine(tally.Answer);
            //   PointPairList list = new PointPairList();
            //   list.Add(tally.Tally, y);

            //   BarItem myCurve = myPane.AddBar(tally.Answer, list, Color.Blue);
            //   myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue, 90);

            //   y++;
            //}
            PointPairList list = new PointPairList();
            foreach (TallyDetail tally in tallies)
            {
               list.Add(tally.Tally, 0);
            }

            BarItem myCurve = myPane.AddBar("", list, Color.Blue);
            myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue, 90);

            // Set BarBase to the YAxis for horizontal bars
            myPane.BarSettings.Base = BarBase.Y;

            // Fill the chart background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White,
               Color.FromArgb(255, 255, 166), 45.0F);

            zgc.AxisChange();

            // Create TextObj's to provide labels for each bar
            BarItem.CreateBarLabels(myPane, false, "f0");
            zgc.Refresh();
         }
         else if (question.Type == QuestionType.Open)
         {

         }
         else if (question.Type == QuestionType.TFQ)
         {
            String[] labels = ParseStringArray(question.PossibleAnswers);
            myPane.YAxis.Scale.TextLabels = labels;
            myPane.YAxis.Type = AxisType.Text;

            AnalyseResponses analysis = new AnalyseResponses();
            ArrayList tallies = analysis.TallyResponses(question.PossibleAnswers, responses);

            //int y = 0;
            //foreach (TallyDetail tally in tallies)
            //{
            //   Console.WriteLine(tally.Answer);
            //   PointPairList list = new PointPairList();
            //   list.Add(tally.Tally, y);

            //   BarItem myCurve = myPane.AddBar(tally.Answer, list, Color.Blue);
            //   myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue, 90);

            //   y++;
            //}
            PointPairList list = new PointPairList();
            foreach (TallyDetail tally in tallies)
            {
               list.Add(tally.Tally, 0);
            }

            BarItem myCurve = myPane.AddBar("", list, Color.Blue);
            myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue, 90);

            // Set BarBase to the YAxis for horizontal bars
            myPane.BarSettings.Base = BarBase.Y;

            // Fill the chart background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White,
               Color.FromArgb(255, 255, 166), 45.0F);

            zgc.AxisChange();

            // Create TextObj's to provide labels for each bar
            BarItem.CreateBarLabels(myPane, false, "f0");
            zgc.Refresh();
         }
         else
         {

         }
      }

      private string[] ParseStringArray(ArrayList list)
      {
         string[] temp = new string[list.Count];

         for (int i = 0; i < list.Count; i++)
         {
            temp[i] = list[i].ToString();
         }

         return temp;
      }
   }
}
